// shop/index/index.js
import { getgemomemberuser, getmemberinfo, getMemberLevel } from '@/api/my'
import { goodsInfoList, goodscollect, goodsTagList } from '@/api/shop'
import {
  bannerList
} from '@/api/home'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    isLoad: false,
    memberPoints: 0,
    currentLevel: '',
    nickName: '',
    isEnd: false,
    pageSize: 10,
    avatarUrl: getApp().globalData.imgUri + '/my/avatarUrl.png',
    memberList: [
      {
        grade: 1,
        gradeName: '黄金会员',
        gradeImg: 'huangjin',
        fontColorName: '#A58130',
        fontColor: '#967643',
        integral: 599,
        total: 1000,
      },
      {
        grade: 2,
        gradeName: '铂金会员',
        gradeImg: 'bojin',
        fontColorName: '#A58130',
        fontColor: '#967643',
        integral: 5999,
        total: 10000,
      },
    ],
    navFilter: [],
    navIndex: 0,
    memberObj: {},
    shopList: [],

    pick_list: [],
    gtId: 0,
  },

  onLoad() {
    // 设置顶部样式
    let t = this;
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#000000',
    })
    this.setData({
      memberObj: this.data.memberList[0],
      isLoad: true,
    })

    getgemomemberuser({}).then((res) => {
      var avatarUrl = res.data.avatarUrl;
      var nickName = res.data.nickName;
      if (res.data.phonenumber) {
        wx.setStorageSync('phone', res.data.phonenumber);
      }
      if (avatarUrl) {
        t.setData({
          avatarUrl: getApp().globalData.cdn + avatarUrl,
        })
      }
      if (nickName) {
        t.setData({
          nickName: nickName,
        });
      }
      t.f_getmemberinfo();
    });

  },
  f_getmemberinfo() {
    let t = this;
    getmemberinfo({}).then((res) => {
      if (res.code == 200) {
        this.setData({
          memberPoints: res.data.shopperInfo.memberPoints,
        });
        t.f_getMemberLevel();
      }
    });
  },
  f_getMemberLevel() {
    let t = this;
    getMemberLevel({}).then((level) => {
      if (level.code == 200) {
        wx.setStorageSync('memberLevel', level.data);
        t.setData({
          currentLevel: level.data.currentLevel,
        });
        t.f_goodsTagList();
      }
    })
  },
  f_goodsTagList() {
    let t = this;
    goodsTagList({ storeType: 1 }).then(res => {
      if (res.code == 200) {
        t.setData({
          navFilter: res.rows,
          gtId: res.rows[0].gtId,
        });
        t.getShopList();

      }
    })
  },
  // 切换筛选
  changeNav(e) {
    let { index } = e.currentTarget.dataset;
    if (index == this.data.navIndex) {
      return false;
    }
    let gtId = this.data.navFilter[index].gtId
    this.setData({
      navIndex: index,
      gtId: gtId,
    }, () => {
      this.getShopList()
    })
  },
  getShopList(pageNum = 1) {
    let t = this;
    if (pageNum == 1) {
      t.setData({
        shopList: [],
        isEnd: false
      })
    }
    let o = {
      pageNum,
      pageSize: t.data.pageSize,
      status: 0,
      gtId: t.data.gtId,
    }
    goodsInfoList(o).then((res) => {
      if (res.code == 200) {
        for (var i = 0; i < res.rows.length; i++) {
          res.rows[i].collect = false
        }
        let list = t.data.shopList.concat(res.rows)
        let isEnd = list.length >= res.total
        t.setData({
          shopList: list,
          isEnd,
        });
        t.getBannerList();
      }
    })
  },
  // 获取首页banner 
  getBannerList() {
    console.log("11111111111");
    let phone = wx.getStorageSync('phone')
    let o = {
      moduleType: 'INTEGRAL_BANNER',
      status: 0
    }

    if (phone) {
      o.gemoLevel = wx.getStorageSync('memberLevel').currentLevelEnum
      o.haveCommonBanner = true
    } else {
      o.onlyCommonBanner = true
    }
    bannerList(o).then(res => {
      if (res.code == 200) {
        this.setData({
          pick_list: res.rows
        })
      }
    });
  },
  // 前往积分明细
  goToIntegral() {
    wx.navigateTo({
      url: '/shop/integral/integral',
    })
  },

  goToExchangeList() {
    wx.navigateTo({
      url: '/shop/exchangeList/exchangeList',
    })
  },

  changeCollect(e) {
    let { id, collected } = e.currentTarget.dataset
    if (collected == false) {
      var collect_update = true
    } else {
      var collect_update = false
    }
    goodscollect({ agiId: id, collect: collect_update }).then((res) => {
      if (res.code == 200) {
        let list = this.data.shopList.map((r) => {
          if (r.agiId == id) {
            r.collected = !r.collected
          }
          return r
        })
        this.setData({
          shopList: list,
        })
      }
    })
  },

  goToDetail(e) {
    let { item } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/shop/detail/detail?id=' + item.agiId,
    })
    // if(item.jumpType == "INNER_APP"){//内部小程序
    //   wx.reLaunch({
    //     url: item.jumpParam,
    //   });
    // }else if(item.jumpType == "OUT_APP"){//外部小程序
    //   wx.navigateToMiniProgram({
    //     appId: item.jumpAppid,
    //     path: item.jumpParam
    //   });
    // }else if(item.jumpType == "WEB_H5"){//
    //   wx.navigateTo({
    //     url: '/shop/webview/webview?webSrc='+item.jumpParam,
    //   })
    // }
  },

  bindscrolltolower() {
    console.log('bindscrolltolower')
    // this.getShopList(1)
    let { isEnd } = this.data
    if (!isEnd) {
      let pageNum = Math.ceil(this.data.shopList.length / this.data.pageSize) + 1
      this.getShopList(pageNum)
    }
  },
  goToBannerDetail(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (item.jumpType != "NONE") {
      wx.setStorageSync('gemoPickItem', item);
      wx.navigateTo({
        url: '/pages/gemoPickDetail/gemoPickDetail',
      });
    }
  },
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})
